import { SocketDecodeData, SocketProtoCoder } from "../../../sgame/Socket/ISocket";

/**
 * socket 的 json 协议编码器, 适用于 websocket(websocket 数据以帧发送，这个编码器没解决粘包拆包)
 */
class JsonProtoCoder extends SocketProtoCoder {
    encode(protoId: number, ...data: any[]): string {
        return JSON.stringify([protoId, ...data]);
    }

    decode(data: string): SocketDecodeData {
        return JSON.parse(data) as SocketDecodeData;
    }
}

// 单例
const jsonProtoCoder = new JsonProtoCoder();
export { jsonProtoCoder, JsonProtoCoder }